package com.jie.langlangmall.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.jie.langlangmall.mapper.SmsFlashPromotionMapper;
import com.jie.langlangmall.model.SmsFlashPromotion;
import com.jie.langlangmall.model.SmsFlashPromotionExample;
import com.jie.langlangmall.service.SmsFlashPromotionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author Jie
 * @Date 2024/11/26
 * @Email 3217998214@qq.com
 * @Description 秒杀、限时活动
 */
@Service
public class SmsFlashPromotionServiceImpl implements SmsFlashPromotionService {

    @Autowired
    private SmsFlashPromotionMapper flashPromotionMapper;

    /**
     * 添加活动
     *
     * @param flashPromotion
     */
    @Override
    public void create(SmsFlashPromotion flashPromotion) {
        flashPromotion.setCreateTime(new Date());
        flashPromotionMapper.insert(flashPromotion);
    }

    /**
     * 更新
     *
     * @param id
     * @param flashPromotion
     */
    @Override
    public void update(Long id, SmsFlashPromotion flashPromotion) {
        flashPromotion.setId(id);
        flashPromotionMapper.updateByPrimaryKey(flashPromotion);
    }

    /**
     * 删除
     *
     * @param id
     */
    @Override
    public void delete(Long id) {
        // TODO: 同时删除 sms_flash_promotion_session表中的信息
        flashPromotionMapper.deleteByPrimaryKey(id);
    }

    /**
     * 上线或者下线
     *
     * @param id
     * @param status
     */
    @Override
    public void updateStatus(Long id, Integer status) {
        SmsFlashPromotion flashPromotion = new SmsFlashPromotion();
        flashPromotion.setId(id);
        flashPromotion.setStatus(status);
        flashPromotionMapper.updateByPrimaryKeySelective(flashPromotion);
    }

    /**
     * 获取详情
     *
     * @param id
     * @return
     */
    @Override
    public SmsFlashPromotion getItem(Long id) {
        return flashPromotionMapper.selectByPrimaryKey(id);
    }

    /**
     * 分页查询
     *
     * @param keyword
     * @param pageSize
     * @param pageNum
     * @return
     */
    @Override
    public List<SmsFlashPromotion> list(String keyword, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        SmsFlashPromotionExample example = new SmsFlashPromotionExample();
        if (!StrUtil.isEmpty(keyword)) {
            example.createCriteria().andTitleLike("%" + keyword + "%");
        }
        return flashPromotionMapper.selectByExample(example);
    }


}
